package org.dnd.temp;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.part.ViewPart;

/**
 * 
 * @author dendy <br>
 *         Jakarta, 2 febuari 2012 </br> <br>
 *         Eclipse Public License</br>
 * 
 */

public class View extends ViewPart {

	Label flabel;
	Label clabel;
	Label rlabel;
	Text fvalue;
	Text cvalue;
	Text rvalue;
	Composite converterComposite;

	public void createPartControl(Composite parent) {

		converterComposite = new Composite(parent, SWT.NULL);
		converterComposite.setLayout(new GridLayout(3, false));

		clabel = new Label(converterComposite, SWT.NULL);
		clabel.setText("Celsius: ");
		cvalue = new Text(converterComposite, SWT.SINGLE | SWT.BORDER);
		GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
		gridData.horizontalSpan = 2;
		cvalue.setLayoutData(gridData);

		flabel = new Label(converterComposite, SWT.NULL);
		flabel.setText("Fahrenheit: ");
		fvalue = new Text(converterComposite, SWT.SINGLE | SWT.BORDER);
		gridData = new GridData(GridData.FILL_HORIZONTAL);
		gridData.horizontalSpan = 2;
		fvalue.setLayoutData(gridData);

		rlabel = new Label(converterComposite, SWT.NULL);
		rlabel.setText("Rheamur: ");
		rvalue = new Text(converterComposite, SWT.SINGLE | SWT.BORDER);
		gridData = new GridData(GridData.FILL_HORIZONTAL);
		gridData.horizontalSpan = 2;
		rvalue.setLayoutData(gridData);

		Button button = new Button(converterComposite, SWT.PUSH);
		gridData = new GridData();
		gridData.horizontalSpan = 3;
		gridData.horizontalAlignment = GridData.CENTER;
		button.setLayoutData(gridData);

		button.setText("Convert Celcius to Fahrenheit and Rheamur");
		button.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event event) {

				String selected = cvalue.getText();
				if (selected == "") {
					fvalue.setText("");
				} else {
					double c = Double.parseDouble(selected);
					double f = (c * 1.8) + 32;
					fvalue.setText(Double.toString(f));
					double r = c * 0.8;
					rvalue.setText(Double.toString(r));
				}
			}
		});

	}

	/**
	 * Passing the focus request to the viewer's control.
	 */
	public void setFocus() {
	}
}